package com.bruce.spring.http.client.controller;

import com.bruce.spring.http.api.Page;
import com.bruce.spring.http.api.Pageable;
import com.bruce.spring.http.api.Store;
import com.bruce.spring.http.api.StoreClient;
import com.bruce.spring.http.client.factory.ClientFactory;
import com.bruce.spring.http.client.util.JsonUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by bruce in 2025/6/17 02:19
 */
@RestController
public class StoreClientController {
    private static final Logger log = LoggerFactory.getLogger(StoreClientController.class);

    private final StoreClient storeClient;
    private final ClientFactory clientFactory;


    public StoreClientController(@Autowired(required = false) StoreClient storeClient,
                                 ClientFactory clientFactory) {
        this.storeClient = storeClient;
        this.clientFactory = clientFactory;
    }


    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    public List<Store> getStores0() {
        StoreClient storeClient2 = clientFactory.getClient("D1", StoreClient.class);
        List<Store> stores = storeClient2.getStores0();
        log.info("getStores0: {}", JsonUtil.toJson(stores));
        return stores;
    }

    @GetMapping("/getStores1")
    public Page<Store> getStores1(Pageable pageable) {
        StoreClient storeClient1 = clientFactory.getClient("D1", StoreClient.class);
        Page<Store> stores = storeClient1.getStores1(pageable);
        log.info("getStores1: {}", JsonUtil.toJson(stores));
        return stores;
    }

    @PostMapping(value = "/postJsonStores")
    public Page<Store> postJsonStores(Store store) {
        log.info("postJsonStores: store:{}", store);
        StoreClient storeClient2 = clientFactory.getClient("D2", StoreClient.class);
        Page<Store> page = storeClient2.postJsonStores(store);
        log.info("postJsonStores: {}", JsonUtil.toJson(page));
        return page;
    }

    @PostMapping(value = "/postFromStores")
    public Page<Store> postFromStores(Store store) {
        StoreClient storeClient2 = clientFactory.getClient("D1", StoreClient.class);
        Page<Store> page = storeClient2.postFormStores(store);
        log.info("postFromStores: page:{}", page);
        return page;
    }

    // Springmvc中可以使用Map<String, Object>接收所有的请求参数, 但是必现要加@RequestBody或者@RequestParam注解
    @PostMapping(path = "/requestMap")
    public Object requestMap(@RequestParam Map<String, String> params, HttpServletRequest request) {
        log.info("request url:{}", request.getRequestURI());
        log.info("request.getQueryString():{}", request.getQueryString());
        log.info("request.getParameterMap():{}", JsonUtil.toJson(request.getParameterMap()));
        log.info("requestMap: params:{}", params);

        StoreClient storeClient2 = clientFactory.getClient("D1", StoreClient.class);
        Store store = storeClient2.requestMap(params);
        log.info("requestMap: store:{}", JsonUtil.toJson(store));
        return store;
    }

    @PostMapping(path = "/requestMapBody")
    public Object requestMapBody(@RequestBody Map<String, String> params, HttpServletRequest request) {
        StoreClient storeClient2 = clientFactory.getClient("D1", StoreClient.class);
        Map<String, String> result = storeClient2.requestMapBody(params);
        log.info("requestMapBody result:{}", JsonUtil.toJson(result));
        return result;
    }

    @PostMapping(path = "/responseEntity", consumes = "application/x-www-form-urlencoded")
    public Object responseEntity(@RequestParam Map<String, String> params) {
        StoreClient storeClient2 = clientFactory.getClient("D1", StoreClient.class);
        ResponseEntity<Store> storeResponseEntity = storeClient2.responseEntity(params);
        HttpHeaders headers = storeResponseEntity.getHeaders();

        ResponseEntity<Store> storeResponseEntity1 = new ResponseEntity<>(storeResponseEntity.getBody(), storeResponseEntity.getStatusCode());

        return storeResponseEntity1;
    }

    @PostMapping(value = "/stores/{storeId}", consumes = "application/json", params = "mode=upsert")
    public Store usePathVars(@PathVariable("storeId") Long storeId, @RequestBody Store store) {
        Store store1 = clientFactory.getClient("D1", StoreClient.class).usePathVars(storeId, store);
        log.info("usePathVars response:{}", store1);
        return store1;
    }

    @DeleteMapping("/stores/{storeId:\\d+}")
    public void delete(Long storeId) {
        log.info("delete storeId:{}", storeId);
        clientFactory.getClient("D1", StoreClient.class).delete(storeId);
        log.info("delete done");
    }


}
